package com.syqc.comlib.rx

import android.os.SystemClock
import android.view.View
import android.widget.EditText
import androidx.core.widget.doAfterTextChanged



fun EditText.doAfterChanged(after: (String) -> Unit) {
    doAfterTextChanged { after(it?.toString() ?: "") }
}


const val FAST_TIME = 500

/**
 * 是否快速点击
 *
 * @return
 */
private fun View.isFastClick(time:Int=FAST_TIME): Boolean {
    val current = SystemClock.elapsedRealtime()
    return if (current - tagTime >= time) {
        tagTime = current
        false
    } else {
        true
    }
}

/**
 * Tag time
 * 记录点击事件
 */
private var View.tagTime: Long
    get() = tag as? Long ?: 0L
    set(value) {
        tag = value
    }

/**
 * 过滤快速点击事件
 *
 * @param block
 * @receiver
 */
fun View.filterFast(block: (View) -> Unit) {
    setOnClickListener { if (!it.isFastClick()) block(it) }
}
/**
 * 过滤快速点击事件
 *
 * @param block
 * @receiver
 */
fun View.filterFast(time:Int,block: (View) -> Unit) {
    setOnClickListener { if (!it.isFastClick(time)) block(it) }
}


